<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="技术名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.isDel" placeholder="是否有效" clearable style="width: 90px" class="filter-item">
        <el-option label="全部" value="" />
        <el-option label="有效" value="1" />
        <el-option label="失效" value="0" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        Search
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        Add
      </el-button>
    </div>
    <el-table
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="Name" width="150px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Description" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.description }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Level" width="150px" align="center">
        <template slot-scope="scope">
          <el-rate v-model="scope.row.level" disabled />
        </template>
      </el-table-column>
      <el-table-column label="State" width="110px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.isDel==1?'有效':'失效' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Actions" align="center" width="250px" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button v-if="row.isDel==1" type="primary" size="mini" @click="detail(row)">
            detail
          </el-button>
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            Edit
          </el-button>
          <el-button v-if="row.isDel==1" size="mini" type="danger" @click="handleModifyStatus(row.id, 0)">
            Delete
          </el-button>
          <el-button v-else size="mini" type="info" @click="handleModifyStatus(row.id, 1)">
            recovery
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getList" />
    <el-dialog v-if="dialogFormVisible" :modal-append-to-body="false" :close-on-click-modal="false" :title="dialogStatus" :visible.sync="dialogFormVisible">
      <Edit :edit-id="editId" :rank="rank" :dialog-form-visible.sync="dialogFormVisible" :parentid="parentId" @getlist="getList" />
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import Edit from '@/views/ability/skill/edit'
export default {
  name: 'SkillIndex',
  components: { Pagination, Edit },
  data() {
    return {
      list: null,
      total: 0,
      listQuery: {
        pageNo: 1,
        pageSize: 10,
        name: '',
        isDel: '',
        id: 0
      },
      editId: undefined,
      parentId: undefined,
      rank: undefined,
      dialogFormVisible: false,
      dialogStatus: '',
      detailVisible: false,
      detailid: undefined
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.get('/skill/listByParent', response => {
        this.list = response.data.records
        this.total = response.data.total
      }, this.listQuery)
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleModifyStatus(id, status) {
      const txt = status === 0 ? '确定要删除该数据吗?' : '确定要恢复该数据吗?'
      this.del('/skill/del', result => {
        this.getList()
      }, { id: id, isDel: status }, txt)
    },
    handleCreate() {
      this.editId = undefined
      this.dialogStatus = '创建技术点'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      this.editId = row.id
      this.dialogStatus = '修改技术点'
      this.dialogFormVisible = true
    },
    detail(row) {
      this.$router.push({
        name: 'SkillDetails',
        query: {
          detailid: row.id
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">
  .filter-container{
    padding-bottom: 10px;
  }
</style>
